<?php
namespace api\xcx\controller;
use think\Cache;
use think\Loader;
use think\Db;
use think\Request;
use think\Controller;
use think\config;
class Moker extends Base
/**
 *  Moker.php 陌客控制器
 *
 * @copyright			(C) 2014-2018 ShengTeng
 * @lastmodify			2018-05-21
 */

{
/**
 * @api {POST} Moker/MokerOrder 生成订单
 * @apiVersion 1.0.0
 * @apiGroup 陌客
 *
* @apiParam {String} token  员工token
* @apiParam {String} goods_id  项目id,逗号隔开
* @apiParam {String} shopId  门店id
* @apiParam {String} goodsPrice  项目金额,逗号隔开
* @apiParam {Int} purePrice  纯色美甲金额
 *
 * @apiSuccess {Int} code 返回码
 * @apiSuccess {String} message  提示信息
 * @apiSuccess {Object} data  返回数据
 *
 * @apiSuccessExample {json} Success-Response:
 *     HTTP/1.1 200 OK
 *     {
 *          "code":0,
 *          "message":"",
 *          "data":{
 *              orderNumber 订单号
 *          }
 *      }
 */
   public function MokerOrder(Request $request){
     $params=$request->param();
     // token
     $token = $this->checkParam($params['token'],'token',true);
     // 项目id,
     $goods_id = $this->checkParam($params['goods_id'],'goods_id',true);
     // 店铺id,
     $shopId = $this->checkParam($params['shopId'],'shopId',true);
     // 项目价格,
     $goodsPrice = $this->checkParam($params['goodsPrice'],'goodsPrice',true);

     // 纯色美甲金额
     $purePrice = $this->checkParam($params['purePrice'],'purePrice',true);
     // 查看员工是否存在
     $staffId = $this->selStaff($token);

     // 生成订单
     $oldPrice = 0;//原价
     $vipPrice = 0;//vip价
     $goodsPrices = explode(',',$goodsPrice);
     $goodsId = explode(',',$goods_id);
      // 原价
     for($i=0;$i<count($goodsPrices);$i++){
         $oldPrice+=$goodsPrices[$i];
     }
      // 根据项目id获取项目折扣和总项目id
      $goiddscount = array();
      $agoid = array();
     // vip价
     $shopConfig = Model('ShopConfig');
     for($i=0;$i<count($goodsId);$i++){
          // 获取每个项目的信息
          $goodsInfo = $shopConfig->selById($goodsId[$i],$shopId);
          if(!$goodsInfo){
            return json(returnAjax('','该店铺无此项目','1'));
          }
          $discount = $goodsInfo['discount'];//折扣
          $serverGoods = $goodsInfo['commondityId'];
          array_push($goiddscount,$discount);
          array_push($agoid,$serverGoods);
        //   if($serverGoods == 1){
        //        $vipPrice+=0;
        //   }else if($serverGoods == 2){
        //        // 获取纯色价格
        //        $vipPrice+=($goodsPrices[$i]-$purePrice)*$discount;
        //   }else{
        //        $vipPrice+=$goodsPrices[$i]*$discount;
        //   }
        $vipPrice+=$goodsPrices[$i]*$discount;
     }
     
     $data['commondityDiscount'] = implode(',',$goiddscount);//项目折扣
     $data['acommondityId'] = implode(',',$agoid);//总项目id
     $data['staffId'] = $staffId['userId'];//员工id
     $data['shopId'] = $shopId;//店铺id
     $data['commodityId'] = $goods_id;//项目id
     $data['commodityPrice'] = $goodsPrice;//项目价格
     $data['amount'] = $oldPrice;//原价
     $data['vipAmount'] = $vipPrice;//vip价
     $data['type'] = 3;//上门状态
     $data['status'] = 0;//订单状态
     $date = getNowDate();
     $data['payStatus'] = 1;//支付状态
     $data['addtime'] = $date;//添加时间
     $data['updtime'] = $date;//修改时间
     $order = Model('Order');
     $ctorder = $order->mokerCOrder($data);
     if($ctorder){
          return json(returnAjax($ctorder));
     }else{
          return json(returnAjax('','系统错误','-1'));
      }
   }

   /**
     * @api {POST} Moker/mokerOrderPayxj 陌客订单支付(现金)
     * @apiVersion 1.0.0
     * @apiGroup 陌客
     *
     * @apiParam {String} token  员工token
     * @apiParam {String} goods_id  项目id,逗号隔开
     * @apiParam {String} shopId  门店id
     * @apiParam {String} goodsPrice  项目金额,逗号隔开
     * @apiParam {Int} purePrice  纯色美甲金额
     *
     * @apiSuccess {Int} code 返回码
     * @apiSuccess {String} message  提示信息
     * @apiSuccess {Object} data  返回数据
     *
     * @apiSuccessExample {json} Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *          "code":0,
     *          "message":"",
     *          "data":{

     *          }
     *      }
     */
    public function mokerOrderPayxj(Request $request){
     $params=$request->param();
     // token
     $token = $this->checkParam($params['token'],'token',true);
     // 项目id,
     $goods_id = $this->checkParam($params['goods_id'],'goods_id',true);
     // 店铺id,
     $shopId = $this->checkParam($params['shopId'],'shopId',true);
     // 项目价格,
     $goodsPrice = $this->checkParam($params['goodsPrice'],'goodsPrice',true);
    // 纯色美甲金额
     $purePrice = $this->checkParam($params['purePrice'],'purePrice',true);

     // 查看员工是否存在
     $staffId = $this->selStaff($token);
     //根据项目id和项目价格算出原价和会员价
     $oldPrice = 0;
     $nowPrice = 0;
     $goodsPrices = explode(',',$goodsPrice);
      // 原价
     for($i=0;$i<count($goodsPrices);$i++){
         $oldPrice+=$goodsPrices[$i];
     }
     $data['staffId'] = $staffId['userId'];//员工id
     $data['shopId'] = $shopId;//店铺id
     $data['commodityId'] = $goods_id;//项目id
     $data['commodityPrice'] = $goodsPrice;//项目价格
     $goodsId = explode(',',$goods_id);

     // 根据项目id获取项目折扣和总项目id
     $goiddscount = array();
     $agoid = array();
     $shopConfig = Model('ShopConfig');
     for($i=0;$i<count($goodsId);$i++){
          // 获取每个项目的信息
          $goodsInfo = $shopConfig->selById($goodsId[$i],$shopId);
          if(!$goodsInfo){
            return json(returnAjax('','该店铺无此项目','1'));
          }
          $discount = $goodsInfo['discount'];//折扣
          $serverGoods = $goodsInfo['commondityId'];
          array_push($goiddscount,$discount);
          array_push($agoid,$serverGoods);
        //   if($serverGoods == 1){
        //       $vipPrice+=0;
        //   }else if($serverGoods == 2){
        //       // 获取纯色价格
        //       $vipPrice+=($goodsPrices[$i]-$purePrice)*$discount;
        //   }else{
        //       $vipPrice+=$goodsPrices[$i]*$discount;
        //   }
        $vipPrice+=$goodsPrices[$i]*$discount;
      }

     $data['commondityDiscount'] = implode(',',$goiddscount);//项目折扣
     $data['acommondityId'] = implode(',',$agoid);//总项目id
     $data['acommondityIncome'] = $goodsPrice;//总项目收入

     $data['amount'] = $oldPrice;//原价
     $data['discountMoney'] =  $oldPrice;//实际支付金额
     $data['paymentWay'] = 2;
     $data['shopIncome'] = $oldPrice;//店铺收入
     $data['vipAmount'] = $vipPrice;//vip价
     $data['systemIncome'] = 0;//平台收入
     $data['type'] = 3;//上门状态
     $data['status'] = 3;//订单状态
     $date = getNowDate();
     $data['paytime'] = $date;//支付时间
     $data['payStatus'] = 2;//支付状态
     $data['addtime'] = $date;//添加时间
     $data['updtime'] = $date;//修改时间
     $order = Model('Order');
     $ctorder = $order->mokerXjOrder($data);
     if($ctorder){
          return json(returnAjax());
      }else{
          return json(returnAjax('','系统错误','-1'));
      }
}
}
